package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.EvalateMapper;
import com.example.online_order.pojo.Evalate;
import com.example.online_order.pojo.Orders;
import com.example.online_order.service.EvalateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class EvalateServiceImpl implements EvalateService {
    @Autowired
    EvalateMapper mapper;

    @Transactional
    @Override
    public List<Evalate> selectAllEva() {
        List<Evalate> evalateDb = mapper.getAllEva();
        if (evalateDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found, "未找到评价");
        }else {
            return  evalateDb;
        }


    }

    @Override
    public List<Evalate> getByUserName(String userName) {
        List<Evalate> evalateDb = mapper.getByUserName(userName);
        if (evalateDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found, "未找到该用户的评价");
        } else {
            return evalateDb;
        }

    }

    @Override
    public List<Evalate> getByUserId(Integer userId) {
        List<Evalate> evalateDb = mapper.getByUserId(userId);
        if (evalateDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found, "未找到该用户的评价");
        }else{
            return evalateDb;
        }

    }

    @Override
    public List<Evalate> getByMealId(Integer mealId) {
        List<Evalate> evalateDb = mapper.getByMealId(mealId);
        if (evalateDb == null) {
            throw new BussinessException(ExceptionType.bus_meal_id_not_found, "未找到该商品的评价");
        }else {
            return evalateDb;
        }

    }

    @Override
    public List<Evalate> getByMealName(String mealName) {
        List<Evalate> evalateDb = mapper.getByMealName(mealName);
        if(evalateDb == null){
            throw new BussinessException(ExceptionType.bus_meal_name_not_found, "未找到该名称的商品的评价");
        }else {
            return evalateDb;
        }
    }


    @Override
    public List<Evalate> getByMealLevel(Integer mealLevel) {
        List<Evalate> evalateDb = mapper.getByMealLevel(mealLevel);
        if(evalateDb == null){
            throw new BussinessException(ExceptionType.bus_meal_level_not_found, "未找到该星级的评价");
        }else {
            return evalateDb;
        }
    }

    @Transactional
    @Override
    public List<Evalate> selectevaLikeUserMeal(String userName, String mealName) {

        List<Evalate> evalateDb = mapper.selectevaLikeUserMeal(userName, mealName);
        if (evalateDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found, "未找到符合条件的评价");
        }else{
            return evalateDb;
        }
    }

    @Transactional
    @Override
    public Integer addEva(Evalate evalate) {
        return mapper.addEva(evalate);

    }

    @Transactional
    @Override
    public Integer deleteEva(Integer id) {
        Evalate evalateDb = mapper.getById(id);
        if( evalateDb== null){
            throw new BussinessException(ExceptionType.bus_id_not_found, "该评价不存在");
        }  else {
            return mapper.deleteEva(id);
        }

    }

    @Transactional
    @Override
    public Integer updateEva(Evalate evalate) {
        Evalate evalateDb = mapper.getById(evalate.getId());
        if( evalateDb== null){
            throw new BussinessException(ExceptionType.bus_id_not_found, "该评价不存在");
        }  else {
            return mapper.updateEva(evalate);
        }


    }


}
